package com.suse.lhw.controller;

import com.suse.lhw.domin.PHealthData;
import com.suse.lhw.domin.PHealthrecord;
import com.suse.lhw.domin.PIfo;
import com.suse.lhw.security.service.PHealthDataService;
import com.suse.lhw.security.service.PHealthrecordService;
import com.suse.lhw.security.service.PIfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.com.suse.lhw.security.controller
 * @ClassName: personController
 * @Description: 用于健康管理
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/15 11:11
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/15 11:11
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
@RestController
@RequestMapping("personUser")
public class personController {
    @Autowired
    private PIfoService pIfoService;
    @Autowired
    private PHealthDataService pHealthDataService;
    @Autowired
    private PHealthrecordService pHealthrecordService;

    @PostMapping("addPersonIfo")
    public String addPersonIfo(PIfo pIfo) {
        /**
         * @Author:Hongwei Li
         * @Description:在用户登录时增加一个用户信息
         * @Date:14:14 2020/5/21
         * @Param:[pIfo]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        pIfo.setUserId(userId);

        if (pIfoService.insertSelective(pIfo) == 0)
            return "添加成功";
        return "添加失败！";
    }

    @PostMapping("updatePersonIfo")
    public String updatePersonIfo(PIfo pIfo) {
        /**
         * @Author:Hongwei Li
         * @Description:修改一个信息
         * @Date:14:22 2020/5/21
         * @Param:[pIfo]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        pIfo.setUserId(userId);
        if (pIfoService.updateByPrimaryKeySelective(pIfo) == 0)
            return "修改失败";
        return "修改成功！";
    }

    @PostMapping("selectPifoByUserid")
    public PIfo selectPifoByUserId(String userId) {
        /**
         * @Author:Hongwei Li
         * @Description:用户获取自己目前的个人信息
         * @Date:14:23 2020/5/21
         * @Param:[userId]
         * @Return:com.suse.lhw.domin.PIfo
         **/
        return pIfoService.selectByUserId(userId);
    }

    @PostMapping("addAhealthdata")
    public String addAHealthData(PHealthData pHealthData) {
        /**已经测试
         * @Author:Hongwei Li
         * @Description:在新增用户的时候增加用户健康信息
         * @Date:16:22 2020/5/21
         * @Param:[pHealthData]
         * @Return:java.lang.String
         **/
        String userId = "aaa";
        pHealthData.setStarttime(new Date());
        if (pHealthDataService.insertSelective(pHealthData) == 0) {
            return "增加基础信息失败！";
        }
        return "增加成功";
    }

    @PostMapping("updateAHealthData")
    public String updateAHealthData(@RequestBody PHealthData pHealthData) {
        /**
         * @Author:Hongwei Li
         * @Description:修改用户将康信息
         * @Date:16:24 2020/5/21
         * @Param:[pHealthData]
         * @Return:java.lang.String
         **/
        System.out.println("the ph is" + pHealthData);
        if (pHealthDataService.updateByPrimaryKeySelective(pHealthData) == 0)
            return "修改失败";
        return "修改成功！";
    }

    @RequestMapping("selectAHealthData")
    public PHealthData selectAHealthData() {
        /**
         * @Author:Hongwei Li
         * @Description:根据用户的id查找他的健康信息
         * @Date:16:29 2020/5/21
         * @Param:[]
         * @Return:com.suse.lhw.domin.PHealthData
         **/
        String userId = "aaa";
        System.out.println("this is phealthdata userid is " + userId);
        System.out.println("**************phealth is " + pHealthDataService.selectByUserid(userId));
        return pHealthDataService.selectByUserid(userId);
    }

    @RequestMapping("selectAHealthRecord")
    public PHealthrecord selectAHealthRecord(int totalFoodCalories, int totalMovementCalories) {
        /**
         * @Author:Hongwei Li
         * @Description:查找一个健康记录，通过今天的日期，用户的id查找，如果没有找到
         *               则重新建立一个，每次新建的时候，根据用户的健康数据，算出今天
         *               需要的热量差。如果用户的健康数据更新，会更改健康记录的status
         *               查找时用户的记录也应该更改，
         *
         * @Date:17:21 2020/5/21
         * @Param:[]
         * @Return:com.suse.lhw.domin.PHealthrecord
         **/
        String userId = "aaa";
        return pHealthrecordService.selectAHealthRecord(userId, totalFoodCalories, totalMovementCalories);
    }

    @RequestMapping("isHRecordExit")
    public boolean isHRecordExit() {
        /**
         * @Author:Hongwei Li
         * @Description:查看今日的健康记录是否存在
         * @Date:20:49 2020/5/21
         * @Param:[]
         * @Return:boolean
         **/
        String userId = "aaa";
        return pHealthrecordService.isHRecordExit(userId, new Date());
    }


}
